package cn.tedu._07vehicle.controller;

import cn.tedu._07vehicle.base.response.Result;
import cn.tedu._07vehicle.base.response.StatusCode;
import cn.tedu._07vehicle.mapper.UserMapper;
import cn.tedu._07vehicle.pojo.dto.UserLoginParam;
import cn.tedu._07vehicle.pojo.dto.UserRegParam;
import cn.tedu._07vehicle.pojo.entity.User;
import cn.tedu._07vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author tune_the last
 * @date 2025/11/14 - 16:05
 */
@Tag(name = "用户管理模块")
@RestController
@RequestMapping("/v1/user/reg")
@Slf4j
public class UserController {
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("/reg")
    public Result register(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("注册功能参数:" + userRegParam);
        //1.接受客户端传递的数据
        /*
            2.数据合法性的校验[]
            3.确认用户名是否被占用[SELECT接口]
            4.注册用户
         */
        //TODO 数据校验
        int count = userMapper.selectUsernameExistence(userRegParam.getUsername());
        if (count > 0){
            return Result.fail(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i = userMapper.userRegister(user);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }

    @Autowired
    private UserMapper userMapper;

    /*
        通过HttpSession会话参数实现，该参数所有方法的操作都是在服务器端的'内存'中实现的
        session.setAttribute(key, value)
        session.getAttribute(key)
        session.removeAttribute(key)
     */

    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("请求获取参数："+ userLoginParam);
        UserVO userVO = userMapper.userLogin(userLoginParam);
        if (userVO == null){
            return Result.fail(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //设置会话标识到服务器的内存中
        session.setAttribute("user", userVO);
        return Result.ok(userVO);
    }

    /*
        获取当前用户信息
     */
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 30)
    @GetMapping("/currentUser")
    public Result currentUser(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        return Result.ok(userVO);
    }

    @Operation(summary = "退出登录功能")
    @ApiOperationSupport(order = 40)
    @GetMapping("/logout")
    public Result logout(HttpSession session){
        session.removeAttribute("user");
        return Result.ok();
    }
}
